<template>
  <div v-loading="loading">
    <el-form ref="form" :model="form" :rules="rules" label-width="120px" class="dataForm">
      <el-row>
        <el-col :span="12">
          <el-form-item :label="$t('赋能类型')" prop="empowerType">
            <el-select v-model="form.empowerType" filterable :placeholder="$t('赋能类型')">
              <el-option
                v-for="dict in empowerTypeOptions"
                :key="dict.dictValue"
                :label="$t(dict.dictLabel)"
                :value="dict.dictValue"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('执行人')" prop="executor">
            <UserSelect
              v-model="form.executor"
              :multiple="true"
              :placeholder="$t('执行人')"
            />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item :label="$t('工作内容')" prop="jobContent">
            <el-input v-model="form.jobContent" type="textarea" :placeholder="$t('工作内容')" show-word-limit maxlength="1000" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('是否完成')" prop="whetherComplete">
            <el-radio-group v-model="form.whetherComplete">
              <el-radio
                v-for="dict in yesOrNoOptions"
                :key="dict.dictValue"
                :label="dict.dictValue"
              >{{ $t(dict.dictLabel) }}</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
        <el-col v-if="form.whetherComplete === 'Y'" :span="12">
          <el-form-item :label="$t('完成日期')" prop="completeDate">
            <DatePicker v-model="form.completeDate" :placeholder="$t('完成日期')" />
          </el-form-item>
        </el-col>
        <el-col v-if="form.whetherComplete === 'Y'" :span="24">
          <el-form-item :label="$t('成果描述')" prop="resultDescribed">
            <el-input v-model="form.resultDescribed" type="textarea" :placeholder="$t('成果描述')" show-word-limit maxlength="1000" />
          </el-form-item>
        </el-col>
      </el-row>
      <!-- 附件列表 -->
      <FileTable :list.sync="form.fileList" has-permi="deal:empower" data-type="deal_after_empower" :business-id="autoBusinessId($route.query.id, form)" :btn-show="!otherBtnHide" />
    </el-form>
    <div slot="footer" class="dialog-footer" style="text-align: right;">
      <!--保存，保存并启动流程，取消 三个按钮-->
      <save-btn has-permi="deal:empower" />
    </div>
  </div>
</template>

<script>
    import { saveDealAfterEmpower, getDealAfterEmpower } from '@/api/deal/empower/dealAfterEmpower'
    export default {
        data() {
            return {
                // 赋能类型字典
                empowerTypeOptions: [], // 表单参数
                form: {},
                // 遮罩层
                loading: false,
              // 是否字典
              yesOrNoOptions: [],
              // 是否隐藏其它按钮
              otherBtnHide: false,
              dealId: this.$route.query.id,
                // 表单校验
                rules: {
                  empowerType: [
                    { required: true, message: this.$t('必填信息'), trigger: 'blur' }
                  ], executor: [
                    { required: true, message: this.$t('必填信息'), trigger: 'blur' }
                  ], jobContent: [
                    { required: true, message: this.$t('必填信息'), trigger: 'blur' }
                  ], whetherComplete: [
                    { required: true, message: this.$t('必填信息'), trigger: 'blur' }
                  ] }
            }
        },
        created() {
            this.getDicts('empower_type').then(response => {
                this.empowerTypeOptions = response.data
            })
            this.getDicts('sys_yes_no').then(response => {
              this.yesOrNoOptions = response.data
            })
        },
        methods: {
            // 初始化数据
            init(data) {
                if (data != null && data.id !== undefined) {
                    getDealAfterEmpower(data.id).then(response => {
                        this.form = response.data
                    })
                } else {
                    this.reset()
                }
            },
            // 表单重置
            reset() {
                this.form = {
                id: undefined,
                dealId: this.dealId,
                companyId: this.$route.query.companyId,
                empowerType: undefined,
                executor: [this.$store.state.user.userId],
                jobContent: undefined,
                whetherComplete: 'N',
                completeDate: undefined,
                resultDescribed: undefined,
                createBy: undefined,
                createTime: undefined,
                updateBy: undefined,
                updateTime: undefined,
                delFlag: undefined,
                fileList: []
                }
                this.resetForm('form')
            },
            // 取消按钮
            cancel() {
                this.$parent.$parent.formOpen = false
                this.$parent.$parent.reset()
            },
            // 关闭form页面
            closeForm() {
                // 关闭form页面遮罩层
                this.loading = false
                // 关闭form页面
                this.$parent.$parent.formOpen = false
                // 刷新list页面
                this.$parent.$parent.getList()
            },
            /** 提交按钮 */
            submitForm: function() {
                this.$refs['form'].validate(valid => {
                    if (valid) {
                        if (this.form.whetherComplete === 'N') {
                          this.form.completeDate = undefined
                          this.form.resultDescribed = undefined
                        }
                        this.loading = true
                        saveDealAfterEmpower(this.form).then(response => {
                            if (response.code === 200) {
                                this.msgSuccess(this.$t('保存成功'))
                                this.closeForm()
                            } else {
                                this.msgError(response.msg)
                            }
                            this.loading = false
                        })
                    } else {
                      this.locationError(document)
                    }
                })
           }
        }

    }
</script>
